package xyz.vmcserver.wdibbs.model.controller;

import com.mysql.cj.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import xyz.vmcserver.wdibbs.model.po.ArticleView;
import xyz.vmcserver.wdibbs.model.po.UserGroup;
import xyz.vmcserver.wdibbs.model.po.UserView;
import xyz.vmcserver.wdibbs.model.service.ArticleService;
import xyz.vmcserver.wdibbs.model.service.ToolsService;
import xyz.vmcserver.wdibbs.model.service.UserGroupService;
import xyz.vmcserver.wdibbs.model.service.UserService;
import xyz.vmcserver.wdibbs.model.vo.Article;
import xyz.vmcserver.wdibbs.model.vo.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserGroupService userGroupService;

    @RequestMapping("/login")
    public String login(){
        return "user/login";
    }



    @RequestMapping("/register")
    public String register(){
        return "user/reg/register";
    }

    @RequestMapping("/logout")
    public String logout(){
        return "user/logout";
    }

    @RequestMapping("/active/{activeID}")
    public String userActivate(@PathVariable("activeID") String activeID){
        if(userService.active(activeID)){
            return "user/reg/activate_done";
        }else{
            return "user/reg/activate_failed";
        }
    }

    @RequestMapping("/login.do")
    public String loginDo(Model model, User user, HttpServletRequest httpServletRequest){
        User fiUser = userService.login(user);//fullInfoUser
        if(null!=fiUser){
            String[] groupInfo = userService.getGroupInfo(fiUser.getGroup_alias());
            if(fiUser.getHeader_url()==null){
                fiUser.setHeader_url("default.jpg");
            }
            if(fiUser.getAlias()==null){
                fiUser.setAlias("用户"+fiUser.getId());
            }
            User respondUser = new User(fiUser.getId(),fiUser.getUsername(),fiUser.getAlias(),groupInfo[1], fiUser.getScore(), fiUser.getHeader_url());
            HttpSession session = httpServletRequest.getSession();
            session.setAttribute("isLoginUser",respondUser);
            session.setAttribute("permission",Integer.valueOf(groupInfo[0]));
            System.out.println("userPermission:"+groupInfo[0]);
            return "redirect:/home";
        }else {
            return "user/login";
        }
    }

    @RequestMapping("/register.do")
    public String registerDo(Model model,User user){
        if(null!=user){
            if(userService.register(user)){
                System.out.println("注册成功！welcome："+user.getAlias());
                return "user/login";
            }else{
                System.out.println("注册失败！");
                return "user/reg/register";
            }
        }else{
            return "user/reg/register";
        }
    }

    @RequestMapping("/groupInfo")
    public ModelAndView getAllUserGroup(){
        ModelAndView modelAndView = new ModelAndView();
        List<UserGroup> list = userGroupService.selectAll();
        modelAndView.addObject("userGroupList",list);
        modelAndView.setViewName("/user/groupInfo");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/getGroupInfo")
    public UserGroup getGroupInfo(String group_alias){
        UserGroup userGroup = userGroupService.selectAllByGroupAlias(group_alias);
        return userGroup;
    }

    @ResponseBody
    @RequestMapping("/getIsLoginUser")
    public User getIsLoginUser(HttpSession session){
        User isLoginUser = (User) session.getAttribute("isLoginUser");
        System.out.println("user header:"+isLoginUser.getHeader_url());
        return isLoginUser;
    }

    @ResponseBody
    @RequestMapping("/getUserPermission")
    public Integer getUserPermission(HttpSession session){
        System.out.println("ajax get permission");
        Integer userPermission = (Integer) session.getAttribute("permission");

        return userPermission;
    }

    @RequestMapping("/flushInfo")
    public void flushUserInfo(HttpSession session){
        User isLoginUser = (User) session.getAttribute("isLoginUser");
        
    }
    @RequestMapping("/{username}")
    public ModelAndView userHome(@PathVariable String username){
        ModelAndView modelAndView = new ModelAndView();
        UserView userView = userService.userInfo(username);
        modelAndView.addObject("userInfoAll",userView);
        modelAndView.setViewName("/user/userHome");
        return modelAndView;
    }

}
